package com.ccys.cms.service.masterData.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.masterData.SymptomService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.masterData.SymptomDelDto;
import com.ccys.db.dto.masterData.SymptomPageQueryDto;
import com.ccys.db.dto.masterData.SymptomSaveDto;
import com.ccys.db.entity.TblSymptom;
import com.ccys.db.mapper.TblSymptomMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.masterData.SymptomPageVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 症状实现类
 * @author: ZhangJingCan
 * @create: 2023-12-12 11:24
 **/

@Service
public class SymptomServiceImpl extends BaseServiceImpl<TblSymptom> implements SymptomService {

    @Resource
    private TblSymptomMapper tblSymptomMapper;

    /**
     * 用于保存修改状态日志
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult save(SymptomSaveDto dto) {
        //校验当前集团并且当前名称存在则返回错误信息 - 同时判断如果带有ID，也就是修改，需要排除当前自身 - 并且是未删除
        if (ObjectUtil.isNotEmpty(dto.getName()) && ObjectUtil.isNotEmpty(dto.getGroupId())) {
            Example example = new Example(TblSymptom.class);
            Example.Criteria criteria = example.createCriteria().andEqualTo("name", dto.getName())
                    .andEqualTo("groupId", dto.getGroupId())
                    .andEqualTo("delFlag", 0);
            //判断是否带有ID
            if (ObjectUtil.isNotEmpty(dto.getId())) {
                //排除掉自身
                criteria.andNotEqualTo("id", dto.getId());
            }
            //执行sql查询
            if (ObjectUtil.isNotEmpty(tblSymptomMapper.selectOneByExample(example))) {
                //返回错误信息
                return ResponseResult.error("The name already exists in the current group!");
            }
        }
        if (ObjectUtil.isNotEmpty(dto.getId()) && ObjectUtil.isNotEmpty(dto.getStatus())) {
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
        }

        //以上校验完成 - 开始新增或者保存 - 并且返回是否成功
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult delete(SymptomDelDto dto) {
        //判断是否传入ID - 未传入抛出错误信息
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备数据 - 软删除
        TblSymptom tblSymptom = new TblSymptom();
        tblSymptom.setId(dto.getId());
        tblSymptom.setDelFlag(1);
        //操作数据库并返回
        if (tblSymptomMapper.updateByPrimaryKeySelective(tblSymptom) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    public ResponseResult findPage(SymptomPageQueryDto dto) {
        //判断值如果不在区间则赋值为null
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        dto.setFrequent(Optional.ofNullable(dto.getFrequent()).filter(f -> f > -1 && f < 2).orElse(null));
        PageDto.startPage();
        PageInfo<SymptomPageVo> info = new PageInfo<>(tblSymptomMapper.findPage(dto));
        return ResponseResult.success(info);
    }
}
